package practisedemo;

import java.util.concurrent.CountDownLatch;

/**
 * @author 兴趣使然黄小黄
 * @version 1.0
 * 演示 CountDownLatch
 */
@SuppressWarnings({"all"})
public class Demo3 {

    public static void main(String[] args) throws InterruptedException {
        // 构造方法中指定有多少个任务需要到达”终点“
        CountDownLatch latch = new CountDownLatch(5); // 声明了需要5个线程

        for (int i = 0; i < 5; i++) {
            Thread t = new Thread(()->{
                try {
                    Thread.sleep(3000);
                    System.out.println(Thread.currentThread().getName() + "到达终点!");
                    latch.countDown();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            t.start();
        }

        // 等待所有的线程到达，否则会一直阻塞
        latch.await();
        System.out.println("所有线程均已经完成！");
    }
}
